package top.wyloop.intoeins.module.iam.initializer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Component;
import top.wyloop.intoeins.module.iam.model.consts.InitPermissionConst;
import top.wyloop.intoeins.module.iam.repository.PermissionRepository;

import java.util.Date;

/**
 * @author Wyatt6
 * @date 2025/2/1
 */
@Slf4j
@Component
@Order(1)
public class PermissionInitializer implements CommandLineRunner {
    @Autowired
    private PermissionRepository repository;

    @Override
    public void run(String... args) throws Exception {
        loadPermissionsOfPermissionResource();
        loadPermissionsOfRoleResource();
        loadPermissionsOfUserResource();
        loadPermissionsOfIamMenu();
        loadPermissionsOfAPI();
    }

    private void loadPermissionsOfPermissionResource() {
        log.info("[[加载permission资源的权限]]");
        try {
            repository.insert(
                    InitPermissionConst.PERMISSION_ADD_ID, "IAM",
                    InitPermissionConst.PERMISSION_ADD,
                    InitPermissionConst.PERMISSION_ADD_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.PERMISSION_ADD);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.PERMISSION_ADD);
        }
        try {
            repository.insert(
                    InitPermissionConst.PERMISSION_DELETE_ID, "IAM",
                    InitPermissionConst.PERMISSION_DELETE,
                    InitPermissionConst.PERMISSION_DELETE_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.PERMISSION_DELETE);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.PERMISSION_DELETE);
        }
        try {
            repository.insert(
                    InitPermissionConst.PERMISSION_EDIT_ID, "IAM",
                    InitPermissionConst.PERMISSION_EDIT,
                    InitPermissionConst.PERMISSION_EDIT_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.PERMISSION_EDIT);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.PERMISSION_EDIT);
        }
        try {
            repository.insert(
                    InitPermissionConst.PERMISSION_QUERY_ID, "IAM",
                    InitPermissionConst.PERMISSION_QUERY,
                    InitPermissionConst.PERMISSION_QUERY_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.PERMISSION_QUERY);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.PERMISSION_QUERY);
        }
    }

    private void loadPermissionsOfRoleResource() {
        log.info("[[加载role资源的权限]]");
        try {
            repository.insert(
                    InitPermissionConst.ROLE_ADD_ID, "IAM",
                    InitPermissionConst.ROLE_ADD,
                    InitPermissionConst.ROLE_ADD_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.ROLE_ADD);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.ROLE_ADD);
        }
        try {
            repository.insert(
                    InitPermissionConst.ROLE_DELETE_ID, "IAM",
                    InitPermissionConst.ROLE_DELETE,
                    InitPermissionConst.ROLE_DELETE_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.ROLE_DELETE);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.ROLE_DELETE);
        }
        try {
            repository.insert(
                    InitPermissionConst.ROLE_EDIT_ID, "IAM",
                    InitPermissionConst.ROLE_EDIT,
                    InitPermissionConst.ROLE_EDIT_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.ROLE_EDIT);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.ROLE_EDIT);
        }
        try {
            repository.insert(
                    InitPermissionConst.ROLE_QUERY_ID, "IAM",
                    InitPermissionConst.ROLE_QUERY,
                    InitPermissionConst.ROLE_QUERY_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.ROLE_QUERY);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.ROLE_QUERY);
        }
    }

    private void loadPermissionsOfUserResource() {
        log.info("[[加载user资源的权限]]");
        try {
            repository.insert(
                    InitPermissionConst.USER_ADD_ID, "IAM",
                    InitPermissionConst.USER_ADD,
                    InitPermissionConst.USER_ADD_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.USER_ADD);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.USER_ADD);
        }
        try {
            repository.insert(
                    InitPermissionConst.USER_DELETE_ID, "IAM",
                    InitPermissionConst.USER_DELETE,
                    InitPermissionConst.USER_DELETE_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.USER_DELETE);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.USER_DELETE);
        }
        try {
            repository.insert(
                    InitPermissionConst.USER_EDIT_ID, "IAM",
                    InitPermissionConst.USER_EDIT,
                    InitPermissionConst.USER_EDIT_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.USER_EDIT);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.USER_EDIT);
        }
        try {
            repository.insert(
                    InitPermissionConst.USER_QUERY_ID, "IAM",
                    InitPermissionConst.USER_QUERY,
                    InitPermissionConst.USER_QUERY_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.USER_QUERY);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.USER_QUERY);
        }
    }

    private void loadPermissionsOfIamMenu() throws Exception {
        log.info("[[加载身份认证管理菜单访问权限]]");
        try {
            repository.insert(
                    InitPermissionConst.MENU_IAM_ID, "IAM",
                    InitPermissionConst.MENU_IAM,
                    InitPermissionConst.MENU_IAM_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.MENU_IAM);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.MENU_IAM);
        }
        try {
            repository.insert(
                    InitPermissionConst.MENU_USER_MANAGE_ID, "IAM",
                    InitPermissionConst.MENU_USER_MANAGE,
                    InitPermissionConst.MENU_USER_MANAGE_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.MENU_USER_MANAGE);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.MENU_USER_MANAGE);
        }
        try {
            repository.insert(
                    InitPermissionConst.MENU_ROLE_MANAGE_ID, "IAM",
                    InitPermissionConst.MENU_ROLE_MANAGE,
                    InitPermissionConst.MENU_ROLE_MANAGE_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.MENU_ROLE_MANAGE);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.MENU_ROLE_MANAGE);
        }
        try {
            repository.insert(
                    InitPermissionConst.MENU_PERMISSION_MANAGE_ID, "IAM",
                    InitPermissionConst.MENU_PERMISSION_MANAGE,
                    InitPermissionConst.MENU_PERMISSION_MANAGE_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.MENU_PERMISSION_MANAGE);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.MENU_PERMISSION_MANAGE);
        }
    }

    private void loadPermissionsOfAPI() throws Exception {
        log.info("[[加载API权限]]");
        try {
            repository.insert(
                    InitPermissionConst.UPDATE_GRANTS_ID, "IAM",
                    InitPermissionConst.UPDATE_GRANTS,
                    InitPermissionConst.UPDATE_GRANTS_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.UPDATE_GRANTS);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.UPDATE_GRANTS);
        }
        try {
            repository.insert(
                    InitPermissionConst.UPDATE_BINDS_ID, "IAM",
                    InitPermissionConst.UPDATE_BINDS,
                    InitPermissionConst.UPDATE_BINDS_NAME, 1, new Date(), null);
            log.info("已加载: {}", InitPermissionConst.UPDATE_BINDS);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {}", InitPermissionConst.UPDATE_BINDS);
        }
    }
}
